package com.hzhr.iot.collect.exception;


import com.hzhr.iot.collect.constant.ExceptionReasonEnum;

/**
 * 服务异常
 * @author 48310
 *
 */
@SuppressWarnings("serial")
public class ServiceException extends Exception {
	private ExceptionReasonEnum exceptionReason;//异常原因
	private String exceptionRemark;//异常备注

	public ServiceException(ExceptionReasonEnum exceptionReason) {
		this.exceptionReason = exceptionReason;
	}

	public ServiceException(ExceptionReasonEnum exceptionReason, String exceptionRemark) {
		this.exceptionReason = exceptionReason;
		this.exceptionRemark = exceptionRemark;
	}

	public ExceptionReasonEnum getExceptionReason() {
		return exceptionReason;
	}
	public String getExceptionRemark() {
		return exceptionRemark;
	}
	public String getTrace() {
		StringBuilder traceSb = new StringBuilder();
		
		StackTraceElement[] stackTraces = this.getStackTrace();
		for (int i = stackTraces.length - 1; i >= 0; i--) {
			StackTraceElement stackTrace = stackTraces[i];
			String className = stackTrace.getClassName();
			if (className.startsWith("com.hzhr.iot") && !className.endsWith("-1")) {
				String methodName = stackTrace.getMethodName();
		    	int lineNumber = stackTrace.getLineNumber();
		    	String runtimeInfo = className + "." + methodName + ":" + lineNumber;
		    	traceSb.append("|" + runtimeInfo);
			}
		}
		return traceSb.toString();
	}

	
	@Override
	public String getMessage() {
		return getTrace() + "|" + exceptionReason.getMessage() + ":" + exceptionRemark;
	}
}